// pages/aduting/retreat/retreat_opration.js
var util = require('../../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hiddenmodalput:true,
    isAgree: false,
    superLus:'0/100',
    deductions:'0.00',
    decratibe:'',
    hiddenmodalre:true,
    total_money:0.00,
    total:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    var that = this;
    wx.getStorage({
      key: 'userInfo',
      success: function (res) {
        that.setData({
          userInfo: res.data,
          orderId: options.orderId,
          retreatDate: options.retreatDate,
          init_deposit: options.init_deposit,
          houseAddress: decodeURIComponent(options.houseAddress),
          retreatId: options.retreatId
        })
        util.getReq("/app/my/checkOutJieZhang", {
          source: that.data.userInfo.source,
          uid: that.data.userInfo.user_id,
          'order_id': that.data.orderId,
          'retreat_room_time': that.data.retreatDate,
          
        }, function (datas) {
          if (datas.code == 0) {
            var subM = 0;// 合计扣款
            var sumM = 0;// 合计退款
            sumM += that.data.init_deposit * 1;
            sumM += datas.params.yingTui;
            for (var i = 0; i < datas.params.list.length;i++){
              subM += datas.params.list[i].sumMoney * 1;

            }
        
            that.setData({
              dataMap: datas.params,
              dataList: datas.params.list,
               sumM: sumM.toFixed(2),
               mbM: (sumM-subM).toFixed(2),
               mbM2: (sumM - subM).toFixed(2),
               subM: subM.toFixed(2),
               subM2: subM.toFixed(2)
            })
          } else {
            wx.showToast({
              title: datas.desc,
            })
          }
        });
        
      }, fail: function () {
        wx.redirectTo({
          url: '/pages/index/index',
        })
      }
    });
  },

  bindAgreeChange: function (e) {
    console.log(e.detail.value);
    this.setData({
      isAgree: !!e.detail.value.length
    });
  },
  bindSuperLus:function(e){
    if(e.detail.value.length<=100){
     
       this.setData({
         superLus: str,
      
       
       })
    }

  },
  bindDeductions:function(e){
    var regNeg = /^[1-9]+\d*(\.\d{0,2})?$|^0?\.\d{0,2}$/;  //浮点数
    console.log(e.detail.value);
    console.log(this.data.subM);
    if (regNeg.test(e.detail.value)){
        this.setData({
          total: e.detail.value,
          subM: Number(e.detail.value) + Number(this.data.subM2),
          mbM: Number(this.data.mbM2) - Number(e.detail.value)
        })

    }else{
      this.setData({
        subM:  Number(this.data.subM2),
        mbM: Number(this.data.mbM2),
        total: 0,
      })

    }
  },
  bindAgree:function(e){
    var title = '退房时间为【' + this.data.retreatDate + '】，本操作说明您与租户之间的退房费用已【线下结清】，' +
      '以上流水记录为【现金流水】。房间立即变为可出租状态';
    this.setData({

      hiddenmodalput:false,
      tipes: title
    })
  },
  //取消同意
  cancelM:function(e){
    this.setData({

      hiddenmodalput: true
    })
  },
  //同意退房
  confirmM:function(e){
    this.setData({
      hiddenmodalput: true,
    })
    var regNeg = /^[1-9]+\d*(\.\d{0,2})?$|^0?\.\d{0,2}$/;  //浮点数
    if (!regNeg.test(this.data.deductions)) {
      wx.showModal({
        title: '错误',
        content: "违约金额输入错误",
        mode: true,
        showCancel: false,
        confirmColor: '#007aff',
      })
      return false;
    }
    wx.showLoading({
      title: '加载中请稍后',
      mask:true,
    })

    util.getReq("/app/my/agreeCheckOutUnderLine", {
      source: this.data.userInfo.source,
      uid: this.data.userInfo.user_id,
      'reading.order_id': this.data.orderId,
      'reading.total_money': this.data.total,
      "reading.retreat_room_id": this.data.retreatId,
      "reading.remark": this.data.subM,
      'reading.ifFdApp': true,
      "jztk": this.data.mbM,
      "flag": 0,
      'user.source': this.data.userInfo.source,
      'user.user_id': this.data.userInfo.user_id,
      'user.user_name': this.data.userInfo.user_name,
      'xy': this.data.isAgree
    }, function (datas) {
      if (datas.code == 0) {
        wx.hideLoading();
        wx.navigateBack({
          //url: '/pages/aduting/retreat/retreat_list',
          delta: 2
        })
      } else if(datas.code==301){
        wx.hideLoading();
        wx.navigateTo({
          url: '/pages/index/index',
        })
      }else{
        wx.hideLoading();
        wx.showToast({

          title: datas.desc,
        })
      }
    });
  },
  //拒绝退房
  bindRefuse:function(e){
      this.setData({
        hiddenmodalre:false

      })

  },
  //拒绝退房确定
  reconfirmM:function(e){
    this.setData({
      hiddenmodalre: true,
    })
    wx.showLoading({
      title: '加载中请稍后',
      mask: true,
    })
    util.getReq("/app/my/refuseCheckOut", {
      source: this.data.userInfo.source,
      uid: this.data.userInfo.user_id,
      'order.order_id': this.data.orderId,
      "order.retreat_room_id": this.data.retreatId,
      'user.source': this.data.userInfo.source,
      'user.user_id': this.data.userInfo.user_id,
      'user.user_name': this.data.userInfo.user_name,
      'order.refuse_reson': this.data.refuse_reson,
      'xy': this.data.isAgree
    }, function (datas) {
      console.log(datas);
      if (datas.code == 0) {
        wx.hideLoading();
        wx.navigateBack({
          //url: '/pages/aduting/retreat/retreat_list',
          delta: 2
        })
      } else if (datas.code == 301) {
        wx.hideLoading();
        wx.navigateTo({
          url: '/pages/index/index',
        })
      } else {
        wx.hideLoading();
        wx.showToast({

          title: datas.desc,
        })
      }
    });
  },
  //拒绝退房取消
  recancelM:function(e){
    this.setData({
      hiddenmodalre: true

    })

  },
  refuse_reson:function(e){
    this.setData({
      refuse_reson: e.detail.value

    })
  }
})